<?php if (!defined('BASEPATH'))exit('No direct script access allowed');
/**
 * 视频控制器
 */
class Video extends MY_Controller {

    function __construct() {
        parent::__construct();
        $this->assign("action", "video");
    }
    //视频首页
    function index() {
        $item = $this->db->query("select * from tan_video where status = 1 order by sort desc limit 1")->row_array();
        redirect("video/".$item['salt']);
        $this->display('video/index.html');
    }
    
    //视频详情页
    function detail($video_salt){
        $item = $this->db->query("select * from tan_video where salt = '$video_salt' and status = 1 order by sort desc limit 1")->row_array();
        if(!$item){//如果视频不存在
            $this->display('deleted/index.html');exit;
        }
        $user_id = $this->user_id;
        if($user_id){
            $users_info = $this->db->query("select head_img from tan_users_info where user_id = '$user_id' limit 1")->row_array();
            $item['head_img'] =  strstr($users_info['head_img'],"http") ? $users_info['head_img'] : $this->img_host.$users_info['head_img'];
        }else{
            $item['head_img'] = "/static/default/temp/head.jpg";
        }

        $result = $this->userinfo ? $this->db->query("select * from tan_video_like where video_id = '".$item['id']."' and user_id ='".$this->user_id."' order by id desc limit 1")->row_array() : array();
        $item['is_like'] = $result ? $result['status'] : 0;
        $item['is_hate'] = $result ? !$result['status'] : 0;
        $item['detail_url'] = site_url("video/".$item['salt']);
        
        $item['is_like_or_hate_invilib'] = $result ? 1 : 0;//判断是否可以进行喜欢或讨厌操作
        $item['video_id'] = encrypt_video_id($item['id']);

        //打赏记录
        $reward = $this->db->select("r.user_id,r.target_id,r.create_time,u.head_img,u.nickname")
            ->from("bbs_reward r")
            ->join("users_info u","u.user_id = r.user_id","left")
            ->where(array("r.target_id"=>$item['id'],"r.target_type"=>'video'))->order_by("r.id desc")->group_by("r.user_id")->get()->result_array();
        foreach ($reward as $key=>$val){
            $reward[$key]['head_img'] = strstr($val['head_img'],"http") ? $val['head_img'] : $this->img_host.$val['head_img'];
            $reward[$key]['space_url'] = site_url("u/".encrypt_user_id($val['user_id']));
        }
        $this->assign("site_title", $item['title']);
        $this->assign('reward', $reward);
        $this->assign('item', $item);
        $this->display('video/detail.html');
    }
    
}
